<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create Shipping upload file </title>
</head>
<body>
<?php
date_default_timezone_set('Australia/Melbourne'); //Set Default time zone by user selection 
$dateToday = date("Y_m_d");

if($_GET['date']!=""){		
		$dateToday = $_GET['date'];	
	}

$shopName = "k-mobile";
$shopName = $_GET['shopname'];
$headstring="Action(SiteID=Australia|Country=AU|Currency=AUD|Version=585|CC=ISO-8859-1),OrderID,ItemID,TransactionID,ShippingStatus,ShipmentTrackingNumber,ShippingCarrierUsed";

$csvFolder = "./csvs/";
$recordFolder = "./recorders/";
$uploadFolder = "./uploads/";

$eBayFileName = $shopName."_".$dateToday.".csv";

$eBayCsv=$csvFolder.$eBayFileName;

$preFix = "km_";
if($shopName=="qitty889"){
	$preFix = "889_";
	}

$recordCsv=$recordFolder.$preFix."recorder_".$dateToday.".csv";

$uploadCsv="upload_".$shopName."_".$dateToday.".csv";
$headArr = explode(",",$headstring);
$fl=fopen($uploadFolder.$uploadCsv,"w");
fputcsv($fl,$headArr);
fclose($fl);

$eBayArr = array();
$recordArr = array();

$fl = fopen($eBayCsv,"r");
$counter = 0;
while(($lineData = fgetcsv($fl,0))!= FALSE){
	if($counter>0)
	if((in_array($lineData[13],$eBayArr)=== false and $lineData[13]!="") or $lineData[13]== "0" ){
		array_push($eBayArr,$lineData[13]);
		array_push($eBayArr,$lineData[0]);
		array_push($eBayArr,$lineData[12]);		
		}
	$counter++;
	}

fclose($fl);

$fl = fopen($recordCsv,"r");
$counter = 0;
while(($lineData = fgetcsv($fl,0))!= FALSE){
	if(in_array($lineData[0],$recordArr)===false and $lineData[0]!=""){
		array_push($recordArr,$lineData[0]);
		array_push($recordArr,$lineData[2]);
		if($lineData[3]!=""){
		array_push($recordArr,$lineData[3]);
		}
		else{
		array_push($recordArr,"0");	
			}
		}
	
	}
fclose($fl);
foreach($eBayArr as $k=>$v){
	
	$uploadLine= array();
	$uploadLine= array_fill(0,7," ");
	$uploadLine[0] ="Status";
	$uploadLine[4] = "1";	
	if($k%3==0){
		$uploadLine[3] = $eBayArr[$k];
		$uploadLine[2] = $eBayArr[$k+2];
		
		$theKey = array_search($eBayArr[$k+1],$recordArr);
		if($theKey!==false){
			if(strlen($recordArr[$theKey +2])>1){
								
				if(substr($recordArr[$theKey+1],0,5)=="**FAY"){
					$uploadLine[5]= $recordArr[$theKey +2];
					$uploadLine[6]= "FASTWAY COURIERS";					
					
					}
				else{
					if(substr($recordArr[$theKey +2],0,4)=="9970"){
						$uploadLine[5]= substr($recordArr[$theKey +2],6);
						}
					else{
						$uploadLine[5]= substr($recordArr[$theKey +2],4);
					}
					
					$uploadLine[6]= "Australia Post";
					}
				}
			else{
				$uploadLine[5]= "";
				}
			
			$fl2 = fopen($uploadFolder.$uploadCsv,"a");
			fputcsv($fl2,$uploadLine);
			fclose($fl2);
						
			
			}
		else{
			
			 echo "DID NOT FIND:".$eBayArr[$k+1]."<br/>";
			}
		
		}
	
	
	}

?>
<a href="./uploads/<?php echo $uploadCsv ?>" target="_blank">Click Here</a> to Download <br />

<pre>
<?php
//echo "ebay Arr<br />";

//var_dump($eBayArr);

//echo "record Arr<br />";
//var_dump($recordArr);
?>
</pre>
</body>
</html>